/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package earleyalgorithm;

import java.util.ArrayList;

/**
 *
 * @author NGOC
 */
public class DottedRule extends Rule {
    private int position;
    private Category activeCategory;
    private int index;
    private ArrayList<DottedRule> bases;

    public DottedRule(Rule rule, int index){
        super(rule.getLeft(), rule.getRight());
        position = 0;
        activeCategory = rule.getRight()[0];
        this.index = index;
        bases = null;
    }

    public DottedRule(DottedRule rule, int position, int index){
        super(rule.getLeft(), rule.getRight());
	if(position < 0 || position > right.length) {
            throw new IndexOutOfBoundsException(
		"illegal position: " + position);
	}

	this.position = position;
	// determine active category
	activeCategory = (position < right.length) ? right[position] : null;
        this.index = index;
        bases = rule.getBases();

    }

    /**
     * @return the position
     */
    public int getPosition() {
        return position;
    }

    /**
     * @param position the position to set
     */
    public void setPosition(int position) {
        this.position = position;
    }

    /**
     * @return the activeCategory
     */
    public Category getActiveCategory() {
        return activeCategory;
    }

    /**
     * @param activeCategory the activeCategory to set
     */
    public void setActiveCategory(Category activeCategory) {
        this.activeCategory = activeCategory;
    }

    /**
     * @return the index
     */
    public int getIndex() {
        return index;
    }

    /**
     * @param index the index to set
     */
    public void setIndex(int index) {
        this.index = index;
    }

    public boolean isEnd(){
        if(position >= right.length && activeCategory == null)
            return true;
        return false;
    }

    /**
     * @return the bases
     */
    public ArrayList<DottedRule> getBases() {
        return bases;
    }

    /**
     * @param bases the bases to set
     */
    public void setBases(ArrayList<DottedRule> bases) {
        this.bases = bases;
    }

    public static DottedRule advancedDot(DottedRule rule){
        return new DottedRule(rule, rule.getPosition() + 1, rule.getIndex());
    }

    public void addBase(DottedRule dottedRule){
        bases.add(dottedRule);
    }

    public boolean isClassType(String type){
        if(activeCategory != null && activeCategory.terminal && activeCategory.name.equals(type))
            return true;
        return false;
    }

    public boolean isPassive(){
        if(activeCategory != null && activeCategory.terminal == false)
            return false;
        return true;
    }

    @Override
    public boolean equals(Object obj){
        if(obj instanceof DottedRule && super.equals(obj)){
            DottedRule rule = (DottedRule)obj;
            if(position == rule.getPosition() && index == rule.getIndex())
                return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 67 * hash + this.position;
        hash = 67 * hash + this.index;
        return hash;
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder(left.toString());
	sb.append(" ->");

	for(int i = 0; i <= right.length; i++) {
            if(i == position) {
		sb.append(" *"); // insert dot at position
            }

            if(i < right.length) {
		sb.append(' '); // space between categories
		sb.append(right[i].toString());
            }
	}
        sb.append(" ,");
        sb.append(index);
	return sb.toString();
    }

   

    /*@Override
    public boolean equals(Object obj) {
	return (obj instanceof DottedRule && super.equals(obj)
				&& position == ((DottedRule)obj).position);
    }

    @Override
    public int hashCode() {
	return (super.hashCode() * (31 + position));
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(left.toString());
	sb.append(" ->");

	for(int i = 0; i <= right.length; i++) {
            if(i == position) {
		sb.append(" *"); // insert dot at position
            }

            if(i < right.length) {
		sb.append(' '); // space between categories
		sb.append(right[i].toString());
            }
	}

	return sb.toString();
    }*/
}
